package com.sky.controller.admin;


import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //分页查询
    @GetMapping("/page")
    @ApiOperation( "分页查询分类列表")
    public Result<PageResult> PageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询分类列表",categoryPageQueryDTO);
        PageResult p = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(p);
    }

    //根据类型查询分类
    @GetMapping("/list")
    @ApiOperation( "根据类型查询分类列表,用于数据回显")
    public Result<List<Category>> selectByType(Integer type){
        log.info("根据类型查询分类列表,用于数据回显",type);
        List<Category> category = categoryService.selectByType(type);
        return Result.success(category);
    }

    @PutMapping
    @ApiOperation( "修改分类")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation( "修改分类状态")
    public Result updateStatus(@PathVariable String status,String id){
        Category category = Category.builder().id((long) Integer.parseInt(id)).status(Integer.parseInt(status)).build();
        log.info("修改分类状态",category);
        categoryService.updateStatus(category);
        return Result.success();
    }

    @PostMapping
    @ApiOperation( "新增分类")
    public Result saveCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类",categoryDTO);
        categoryService.saveCategory(categoryDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation( "删除分类")
    public Result deleteCategory(String id){
        log.info("删除分类",id);
        categoryService.deleteCategory(id);
        return Result.success();
    }


}
